<template>
  <div class="auto"></div>
</template>

<script>
import { Dialog } from 'vant'
export default {
  name: "auto",
  methods: {
    GetUrlParame (parameName) {
      var parames = window.location.href
      if (parames.indexOf(parameName) > -1) {
        var parameValue = ''
        parameValue = parames.substring(parames.indexOf(parameName), parames.length)
        if (parameValue.indexOf('&') > -1) {
          parameValue = parameValue.substring(0, parameValue.indexOf('&'))
          parameValue = parameValue.replace(parameName + '=', '')
          return parameValue
        }
        return parameValue
      }
    },
  },
  async created () {
    this.$toast.loading({
      message: "授权中...",
      forbidClick: true,
      overlay: true,
      duration: 0
    })
    const murl = this.GLOBAL.domain
    const url = localStorage.getItem("now_url")
    const code = this.GetUrlParame('code') // 截取code  
    if (!code) {
      var domine = window.location.href.split("#")[0] // 微信会自动识别#    并且清除#后面的内容
      let setWxcodeUrl = await this.axios.post(this.api.setWxcodeUrl, { redirect_uri: domine })
      if (setWxcodeUrl.code == 200) {
        window.location.href = setWxcodeUrl.data
      }
    } else {
      let wxres = await this.axios.post(this.api.wxauth, { code })
      if (wxres.code == 200) {
        let userdata = {
          nickname: wxres.data.userinfo.nickname,
          headimgurl: wxres.data.userinfo.headimgurl,
        }
        localStorage.setItem('userinfo', JSON.stringify(userdata))
        localStorage.setItem("token", wxres.data.token)
        window.location.replace(murl + '/#' + url)
      }
    }
  }

}
</script>
